package com.jsyl.lwbk.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jsyl.lwbk.business.constant.IntegrationConstant;
import com.jsyl.lwbk.business.domain.ChildProjectConfig;
import com.jsyl.lwbk.business.domain.Integration;
import com.jsyl.lwbk.business.mapper.ChildProjectConfigMapper;
import com.jsyl.lwbk.business.mapper.IntegrationMapper;
import com.jsyl.lwbk.business.service.IntegrationService;
import com.jsyl.lwbk.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Fitch
 * 2022/2/9
 */
@Service
public class IntegrationServiceImpl implements IntegrationService {
    @Autowired
    private IntegrationMapper integrationMapper;
    @Autowired
    private ChildProjectConfigMapper childProjectConfigMapper;

    @Override
    public List<Integration> findList(Integration integration) {
        List<Integration> list = integrationMapper.getPage(integration.getName());
        if (!CollectionUtils.isEmpty(list)) {
            for (Integration i : list) {
                i.setAge("-");
                i.setGrade("-");
                if (i.getStartAge() != -1 && i.getEndAge() != -1) {
                    i.setAge(i.getStartAge() + "-" + i.getEndAge());
                }
                if (i.getStartGrade() != -1 && i.getEndGrade() != -1) {
                    i.setGrade(IntegrationConstant.GRADE_MAP.get(i.getStartGrade()) + "年级-" + IntegrationConstant.GRADE_MAP.get(i.getEndGrade()) + "年级");
                }
            }

        }
        return list;
    }

    @Override
    public Map<String, String> getCodeName() {
        List<Integration> list = integrationMapper.findList(null);
        Map<String, String> result = new HashMap<>();
        for (Integration i : list) {
            result.put(i.getCode(), i.getName());
        }
        return result;
    }

    @Override
    public List<Integration> getSelectList() {
        List<Integer> leafType = new ArrayList<>();
        leafType.add(4);
        leafType.add(2);
        return integrationMapper.findList(leafType);
    }

    @Override
    public List<Integration> findListByCode(List<String> codes) {
        return integrationMapper.findListByCode(codes);
    }

    @Override
    public String getCodeName(String projectCode) {
        return integrationMapper.getCodeName(projectCode);
    }

    @Override
    public Integration selectById(Long id) {
        return this.integrationMapper.selectById(id);
    }

    @Override
    public int updateById(Integration integration) {
        //修改起始年龄时，检查是否有小孩未初始化能力配置
        int result = this.integrationMapper.update(integration);
        if (result > 0) {
            initConfig(integration);
        }
        return result;
    }

    @Async
    public void initConfig(Integration integration) {
        List<Long> childIds = this.integrationMapper.listNoConfigChiIds(integration.getCode(), integration.getStartAge(), integration.getEndAge());
        for (Long childId : childIds) {
            int count = this.childProjectConfigMapper.selectCount(childId);
            if (count > 0) {
                ChildProjectConfig config = new ChildProjectConfig();
                config.setId(IdWorker.getId());
                config.setChildId(childId);
                config.setProjectCode(integration.getCode());
                config.setProjectName(integration.getName());
                config.setDifficultyLevel(1);
                config.setCompleteTimes(0);
                config.setStatus(0);
                config.setCreateTime(DateUtils.getNowDate());
                this.childProjectConfigMapper.insert(config);
            }
        }

    }
}
